package com.hzit.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;

/**
 *
 */
@Component
public class MyFilter extends ZuulFilter {

    /**
     * 过滤器类型
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {

        return 0;
    }

    /**
     * 执行该过滤器
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }


    /**
     * 业务逻辑
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        System.out.println("--------执行pre过滤器---------------");

        //获取request
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();

        String token = request.getParameter("token");
        if(StringUtils.isEmpty(token)){

            requestContext.setResponseBody("token不能为空");
            requestContext.setResponseStatusCode(501);
            requestContext.setSendZuulResponse(false);
        }

        // 限流， 权限认证， 加密解密

        return null;
    }
}
